/*
 * @Descripttion: 
 * @version: 
 * @Author: weni09
 * @Date: 2021-04-17 21:54:15
 * @LastEditors: weni09
 * @LastEditTime: 2021-06-12 20:42:51
 * @FilePath: \icoder-frontend\blog\src\store\index.js
 */
import Vue from 'vue';
import Vuex from 'vuex';
import {getTimeInterval} from '../utils/index';
import createPersistedState from 'vuex-persistedstate';
import modules from './modules';
Vue.use(Vuex)
// 系统运行时间
const runAt = '1619018374201';
let timer = null;
const state = {
    runTimeInterval: '',
    hasNextPage:true,
    viewTime: {},
    loginOrRegister:{ type:"login",text:"登录"},
    artDetail:{},
    logResDialogVisible:false,
    
    
}
const mutations = {
    SET_HAS_NEXTPAGE:(state,v) => {
        state.hasNextPage = v;
    },
    GET_RUNTIME_INTERVAL: (state) => {
        if (!timer || !state.runTimeInterval) {
            clearInterval(timer)
            timer = setInterval(() => {
                state.runTimeInterval = getTimeInterval(runAt);
            }, 1000);
        }
    },
    SET_VIEW_TIME:(state,v) => {
        for (let i in v){
            state.viewTime[i] = v[i]
        }
    },
    SET_LOGIN_REGISTER:(state,v) => {
        state.loginOrRegister = v;
    },
    SET_ARTICLE_DETAIL:(state,v) => {
        state.artDetail = v;
    },
    SET_LOGRES_DIALOG:(state,v) => {
        state.logResDialogVisible = v;
    },
}
const actions = {
    setHasNextPage:({commit}, v) => {
        commit('SET_HAS_NEXTPAGE',v)
    },
    initComputeTime: ({commit}) => {
        commit('GET_RUNTIME_INTERVAL');
    },
    setArticleDetail:({commit},v) => {
        commit('SET_ARTICLE_DETAIL',v);
    },
    setLogResDialog:({commit},v) => {
        commit('SET_LOGRES_DIALOG',v);
    },

}
const getters = {
    hasNextPage: state => state.hasNextPage,
    runTimeInterval: state => state.runTimeInterval,
    viewTime: state => state.viewTime,
    loginOrRegister: state => state.loginOrRegister,
    artDetail: state => state.artDetail,
    logResDialogVisible: state => state.logResDialogVisible,
};


// const vuexSession = createPersistedState({
//      //持久化数据到sessionStorage中
//     storage: window.sessionStorage,
//     paths:['auth.auth']
// });
const vuexLocal = createPersistedState({
    //持久化数据到localStorage中
    storage: window.localStorage,
    paths:['viewTime','auth.auth']
});
export default new Vuex.Store({
    namespaced:true,
    state,
    mutations,
    actions,
    modules: modules,
    getters,
    plugins: [vuexLocal]
})